<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kasuss extends CI_Controller {

    var $kasus_id;
    var $status;
    var $cari;
    var $message;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->auth->restrict();

        $this->load->helper('text');
        $this->load->model(array('m_kasus', 'm_santri', 'm_kat_kasus', 'm_sub_kasus'));
        $uri = $this->uri->uri_to_assoc(3);

        if (isset($uri['id'])) {
            $this->kasus_id = $uri['id'];
        }

        if (isset($uri['status'])) {
            $this->status = $uri['status'];
        }

        if (isset($_POST['in_cari'])) {
            $this->cari = $this->input->post('in_cari');
            $this->session->set_userdata('cari', $this->cari);
        }

        if ($this->session->userdata('cari')) {
            $this->cari = $this->session->userdata('cari');
        }

        if ($this->auth->cek_role_member() != '1') {
            redirect('homes');
        }
    }

    public function index($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_kasus->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/kasuss/index/page/';
        $config['total_rows'] = $this->m_kasus->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_kasus->num_pages = $config['per_page'];
        $this->m_kasus->offset = $this->uri->segment(4);
        $this->m_kasus->paginate = TRUE;
        $this->pagination->initialize($config);

        $kasuss = $this->m_kasus->get();
        $data['title'] = $this->lang->line('menu_kasus') . ' ' . $this->lang->line('webname');
        $data['kasuss'] = $kasuss;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('kasus_view', $data);
        $this->load->view('view_footer', $data);
    }

    public function view() {
        $this->session->unset_userdata('cari');

        redirect('kasuss/');
    }

    public function add($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_tanggal', $this->lang->line('tanggal'), 'trim|required');
        $this->form_validation->set_rules('in_kat_kasus', $this->lang->line('kat_kasus'), 'trim|required');
        $this->form_validation->set_rules('in_sub_kasus', $this->lang->line('sub_kasus'), 'trim|required');
        $this->form_validation->set_rules('in_santri', $this->lang->line('santri'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $kat_kasuss = $this->m_kat_kasus->get_kategori_menu();
            $santris = $this->m_santri->get_kategori_menu();
            //$sub_kasuss = $this->m_sub_kasus->get_kategori_menu();

            $data['title'] = $this->lang->line('menu_kasus') . ' ' . $this->lang->line('webname');
            $data['kasus_id'] = $this->kasus_id;
            $data['kat_kasuss'] = $kat_kasuss;
            $data['santris'] = $santris;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('kasus_form', $data);
            $this->load->view('view_footer_kasus', $data);
        } else {
            $this->m_kasus->kasus_santri = $this->input->post('in_santri');
            $this->m_kasus->kasus_kat_kasus = $this->input->post('in_kat_kasus');
            $this->m_kasus->kasus_sub_kat_kasus = $this->input->post('in_sub_kasus');
            $this->m_kasus->kasus_keterangan = $this->input->post('in_keterangan');
            $this->m_kasus->kasus_status = $this->input->post('in_status');
            $this->m_kasus->kasus_tanggal = date('Y-m-d', strtotime($this->input->post('in_tanggal'))); //date('Y-m-d H:i:s');

            $this->m_kasus->kasus_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_kasus->kasus_dirubah = date('Y-m-d H:i:s');

            $this->m_kasus->kasus_id = '';
            $this->m_kasus->save();

            $this->view();
            //redirect('kasuss/');
        }
    }

    public function edit($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_tanggal', $this->lang->line('tanggal'), 'trim|required');
        $this->form_validation->set_rules('in_kat_kasus', $this->lang->line('kat_kasus'), 'trim|required');
        $this->form_validation->set_rules('in_sub_kasus', $this->lang->line('sub_kasus'), 'trim|required');
        $this->form_validation->set_rules('in_santri', $this->lang->line('santri'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $kat_kasuss = $this->m_kat_kasus->get_kategori_menu();
            $santris = $this->m_santri->get_kategori_menu();
            //$sub_kasuss = $this->m_sub_kasus->get_kategori_menu();

            $data['title'] = $this->lang->line('menu_kasus') . ' ' . $this->lang->line('webname');
            $data['kasus_id'] = $this->kasus_id;
            $data['kasuss'] = $this->m_kasus->get_by_id($this->kasus_id);
            $data['kat_kasuss'] = $kat_kasuss;
            $data['santris'] = $santris;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('kasus_form', $data);
            $this->load->view('view_footer_kasus', $data);
        } else {
            $this->m_kasus->kasus_santri = $this->input->post('in_santri');
            $this->m_kasus->kasus_kat_kasus = $this->input->post('in_kat_kasus');
            $this->m_kasus->kasus_sub_kat_kasus = $this->input->post('in_sub_kasus');
            $this->m_kasus->kasus_keterangan = $this->input->post('in_keterangan');
            $this->m_kasus->kasus_status = $this->input->post('in_status');
            $this->m_kasus->kasus_tanggal = date('Y-m-d', strtotime($this->input->post('in_tanggal'))); //date('Y-m-d H:i:s');

            $this->m_kasus->kasus_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_kasus->kasus_dirubah = date('Y-m-d H:i:s');

            $this->m_kasus->kasus_id = $this->kasus_id;
            $this->m_kasus->save();
            //echo 'tgl' . $this->input->post('in_tanggal') . 'tgl2' . $this->m_kasus->kasus_tanggal ;
            //$this->m_kasus->kasus_tanggal = date('Y-m-d', strtotime($this->input->post('in_tanggal'))); //date('Y-m-d H:i:s');
            $this->view();
        }
    }

    public function delete() {
        $this->m_kasus->kasus_id = $this->kasus_id;
        $this->m_kasus->delete();

        $this->view();
    }

    public function get_sub_kasus_from_kat_id($kat) {
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($this->m_sub_kasus->get_sub_kasus_from_kat_kasus($kat)));
    }

}

/* End of file kasuss.php */
/* Location: ./application/controllers/kasuss.php */